package cn.baddonkey.util;

import cn.baddonkey.settings.Settings;
import cn.baddonkey.ui.MainDialog;

import java.util.HashMap;
import java.util.Map;

public class TransApi {
    private static final String TRANS_API_HOST = "http://api.fanyi.baidu.com/api/trans/vip/translate";

    public String getTransResult(String query, String from, String to) throws Exception {
        Map<String, String> params = buildParams(query, from, to);
        return HttpUtils.doGet(TRANS_API_HOST, params);
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        String appid = Settings.getInstance().getSettingMap().get("accessKey");
        String securityKey = Settings.getInstance().getSettingMap().get("secretKey");
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        // 加密前的原文
        String src = appid + query + salt + securityKey;
        params.put("sign", MD5.md5(src));

        return params;
    }

}
